import datetime
import os.path
import sys

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager


class Driver:
    dreiver = ""
    def __init__(self):
        options = webdriver.ChromeOptions()
        self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)
        self.driver.implicitly_wait(2)
    def getScreenShoot(self):
        # dirname = datetime.datetime.now().strftime("%Y-%m-%d")
        # if not os.path.exists("../images/"+dirname):
        #     os.mkdir("../images/"+dirname)
        # filename = sys._getframe().f_back.f_code.co_name+"-"+datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S")+".png"
        # self.driver.save_screenshot("../images/"+dirname+"/"+filename)
        #
        # datename="autoTest"+datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S")+'.png'
        # if not os.path.exists("../images/"+datename):
        #     os.mkdir("../images/"+datename)
        # filename =sys._getframe().f_back.f_code.co_name
        # self.driver.save_screenshot("../images/"+datename+"/"+filename)
        # def getScreenShoot(self):
            # 生成文件夹名称（按日期）
            dirname = datetime.datetime.now().strftime("%Y-%m-%d")
            # 确保 images 目录存在
            if not os.path.exists("../images"):
                os.mkdir("../images")
            # 确保日期文件夹存在
            if not os.path.exists(f"../images/{dirname}"):
                os.mkdir(f"../images/{dirname}")
            # 生成文件名（函数名 + 时间戳）
            filename = sys._getframe().f_back.f_code.co_name + "-" + datetime.datetime.now().strftime(
                "%Y-%m-%d-%H%M%S") + ".png"
            # 保存截图
            screenshot_path = f"../images/{dirname}/{filename}"
            self.driver.save_screenshot(screenshot_path)
            print(f"Screenshot saved to: {screenshot_path}")

GobangDriver = Driver()